var federalpurchases purchases1 purchases2 avmarkup  g1ramey g2ramey mkup1 mkup2 g1 g2 y1 y2 pi1 pi2 tau y g pi i c ernesto p2 ytest realrate c_output g_output pi_c i_ann eeh1 eeh2 eehT db dtaxh pi_g pi1_g pi2_g;

varexo eps_g1 eps_g2;

parameters n gy zeta gamma rho_g1 rho_g2 sigma varphi beta phi_pi alpha1 alpha2 eta taylor share mu phi_b phi_g a3 aux Ac AT std1 std2;

varphi    = 4;      // inverse of Frisch elasticity 
beta      = 0.997;   // monthly discount factor 
sigma     = 1;      // inverse IES
phi_pi    = 1.5;    // Taylor rule
eta       = 1;      // Elasticity of substitution
taylor    = 1;      // if true: taylor rule; if 0: inflation targeting

/* key parameters */
std1      = .1;   // std shock1 
std2      = .1;   // std shock2  
rho_g1    = .65;    // government spending shock persistence
rho_g2    = .73;
alpha1    = .78;   //   Calvo sector 1
alpha2    = .89;     //  Calvo sector 2
gy        = .2;     // G/Y
zeta      = 1-gy;    // share of private consumption in GDP
gamma     = .26;     //.3 public consumption share sector 1
n         = .65;     //.85 size of sector 1    

share = 0.2;              // share of HTM agents on consumption and labor in s.s.
mu = 1.2;            // gross s.s. markup
          // Th/P*Ch in s.s. (share of HTM lump-sum taxes on their nominal consumption). Note a1+a2 = 1 + a3
phi_g = .1;        // elast of HTM taxes on Gov nominal spending
phi_b = .05;        // elast of HTM taxes on Gov debt stock

a3 = (mu*zeta)^-1-1;
aux = 1 + varphi + a3;
Ac = (1+varphi)*(1+a3)*aux^-1;
AT = varphi*a3*aux^-1;

model;
#omega = (n - gamma*(1-zeta))/zeta;
#a1 = n*(mu*zeta)^-1;           // L1h*W1/P*Ch in s.s. (share of HTM labor income from sector 1 on their nominal consumption)
#a2 = (1-n)*(mu*zeta)^-1;            // L2h*W2/P*Ch in s.s.
#A1 = (1+varphi)*varphi*a1*aux^-1;
#A2 = (1+varphi)*varphi*a2*aux^-1;

/* Market clearing in each sector */
n*y1 = - eta*omega*zeta*(1-omega)*tau + omega*zeta*c  + gamma*(1-zeta)*g1;    
(1-n)*y2 = eta*(1-omega)*zeta*omega*tau + (1-omega)*zeta*c + (1-gamma)*(1-zeta)*g2;

/* Phillips curves */
alpha1*pi1 = alpha1*beta*pi1(+1) + (1-alpha1)*(1-alpha1*beta)*(sigma*c + varphi*y1-(1-omega)*tau);       
alpha2*pi2 = alpha2*beta*pi2(+1) + (1-alpha2)*(1-alpha2*beta)*(sigma*c + varphi*y2+omega*tau);       

/* aggregate demand and aggregation */
phi_pi*pi_c - taylor*(i - 0*y) = 0;
/*pi2 = 0;*/

eeh1 = (share*A1*sigma/(1-share*Ac))*(y1(+1) - y1);
eeh2 = (share*A2*sigma/(1-share*Ac))*(y2(+1) - y2);
eehT = (share*AT*sigma/(1-share*Ac))*(dtaxh(+1) - pi_c(+1));
sigma*c = sigma*c(+1) - ((1-share)/(1-share*Ac))*(i - pi_c(+1)) - eeh1 - eeh2 + eehT;
pi_c = omega*pi1 + (1-omega)*pi2;
pi = n*pi1 + (1-n)*pi2;
y  = n*y1  + (1-n)*y2;
g  = gamma*g1 + (1-gamma)*g2;
tau = tau(-1) + pi1 - pi2;    

/* Government block */
purchases1 = rho_g1*purchases1(-1) + std1*eps_g1;  
purchases2 = rho_g2*purchases2(-1) + std2*eps_g2;  
g1 = 0.16*purchases1;
g2 = 0.16*purchases2; 
//g1 = rho_g1*g1(-1) + eps_g1/gamma/gy;
//g2 = rho_g2*g2(-1) + eps_g2/(1-gamma)/gy;
dtaxh = phi_b*(db(-1)) + phi_g*(g - g(-1) + pi_g);
db = beta^-1*db(-1) + (1-zeta)*(g - g(-1) + pi_g - dtaxh);
pi_g =  gamma*pi1_g + (1-gamma)*pi2_g;
pi1_g = (c - c(-1)) + pi_c + varphi*(y1 - y1(-1));
pi2_g = (c - c(-1)) + pi_c + varphi*(y2 - y2(-1));


federalpurchases = purchases1 + purchases2; /* facilitates representation for different runs: either one or the other */

        
pi2 = p2 - p2(-1);
ernesto = tau + p2/omega;

ytest = g + c;
realrate = i - ( omega*pi1(1) + (1-omega)*pi2(1) );

c_output = c*zeta;
g_output = g*(1-zeta);

g1ramey = gamma/n*(1-zeta)*g_output;
g2ramey = (1-gamma)/(1-n)*(1-zeta)*g_output;

i_ann = 12*i;

mkup1 = -(sigma*c + varphi*y1-(1-omega)*tau);
mkup2 = -(sigma*c + varphi*y2+omega*tau);
avmarkup = n*mkup1 + (1-n)*mkup2;

end;

shocks;

var eps_g1 = 1;
var eps_g2 = 1;

end;

/*************************************************************************/

check;

//stoch_simul

//Deterministic simulation


stoch_simul(irf=25,order=1,periods=200,nofunctions,nodisplay);
/* simul(periods=1000);*/



/*
loops(1,xx)=oo_;
end

save('loops.mat');
*/